package Ep01_String.Offer_05;

/**
 * @author Jimmy Zhan WORKSTATION
 * @since 2023/7/13 13:16
 * 说明：
 */

public class Mine {
    public String replaceSpace(String s) {
        if (s.equals("") || s.length() == 0) return s;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' '){
                sb.append("  ");
            }
        }

        int left = s.length() - 1;
        s += sb.toString();
        int right = s.length() - 1;

        char[] ch = s.toCharArray();

        while(left < right) {
            if (ch[left] != ' ') {  // 正常字符
                ch[right] = ch[left];
                left--;
                right--;
            } else {    // 遇到了空格，替换
                ch[right] = '0';
                ch[right - 1] = '2';
                ch[right - 2] = '%';
                left--;
                right -= 3;
            }

        }
        return new String(ch);
    }
}
